const
HIDE_ERROR_INFO_DELAY_TIME = 5000;
var ShareButton = {
	pin : null,
	init : function() {
		var b = document.querySelectorAll("#pin-done .share-button");
		for ( var a = 0; a < b.length; a++) {
			b[a]
					.addEventListener(
							"click",
							function() {
								if (this.classList.contains("disabled")) {
									return
								}
								if (!ShareButton.pin) {
									return
								}
								var d = "http://" + DOMAIN + "/pins/"
										+ ShareButton.pin.pin_id;
								var h = Utils.getImgUrl(ShareButton.pin.file,
										"fw554");
								var g = ShareButton.pin.raw_text;
								var e = [ "url=" + encodeURIComponent(d) ];
								if (this.id == "share_sinaweibo") {
									var c = "http://v.t.sina.com.cn/share/share.php?";
									e.push("&appkey=2499394483");
									e.push("&pic=" + encodeURIComponent(h));
									e.push("&ralateUid=2493118952");
									e.push("&title=" + encodeURIComponent(g))
								} else {
									if (this.id == "share_douban") {
										var c = "http://www.douban.com/recommend/?";
										e.push("&title="
												+ encodeURIComponent(g));
										e.push("&comment="
												+ encodeURIComponent(g))
									} else {
										if (this.id == "share_qzone") {
											var c = "http://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?";
											e.push("&title="
													+ encodeURIComponent(g));
											e.push("&pics="
													+ encodeURIComponent(h))
										}
									}
								}
								e = e.join("");
								(function(i, k, j) {
									function f() {
										if (!window
												.open(
														[ c, e ].join(""),
														"mb",
														[
																"toolbar=0,status=0,resizable=1,width=620,height=450,left=",
																(i.width - 620) / 2,
																",top=",
																(i.height - 450) / 2 ]
																.join(""))) {
											u.href = [ c, e ].join("")
										}
									}
									if (/Firefox/.test(navigator.userAgent)) {
										setTimeout(f, 0)
									} else {
										f()
									}
								})(screen, document, encodeURIComponent)
							})
		}
	},
	setPin : function(a) {
		ShareButton.pin = a
	}
};
var UploadUI = {
	currentSite : "",
	uploading : false,
	sites : {},
	registerSite : function(b, a) {
		this.sites[b] = a
	},
	getSiteObject : function(a) {
		return this.sites[a]
	},
	init : function(a) {
		UploadUI.registerSite(Huaban.siteId, Huaban);
		if (a) {
			Huaban.currentUserId = a.id
		}
		document.querySelector("#bookmarklet .header .close").addEventListener(
				"click", function() {
					UploadUI.closeDialog()
				});
		UploadUI.initSave()
	},
	initSave : function() {
		var b = $("btnSave");
		var a = $("closeSavePanel");
		if (!b) {
			return
		}
		b.addEventListener("click", function() {
			var e = photoshop.getDataUrl();
			var d = $("savePanel");
			if (parseInt(d.style.height) > 0) {
				return
			}
			var c = new Image();
			c.src = e;
			d.querySelector("div.img").appendChild(c);
			c.title = $("description").value + ".png";
			c.alt = $("description").value + ".png";
			c.setAttribute("draggable", "true");
			c.setAttribute("data-downloadurl", "image/png:" + c.title + ".png:"
					+ e);
			d.style.display = "block";
			d.style.height = "115px";
			c.addEventListener("dragstart", function(f) {
				f.dataTransfer.setData("DownloadURL", this.dataset.downloadurl)
			})
		});
		a.addEventListener("click", function() {
			var d = $("savePanel");
			var c = d.querySelector("div.img");
			c.removeChild(c.querySelector("img"));
			d.style.height = "0px"
		})
	},
	showErrorInfo : function(a) {
		$("msg").innerHTML = a;
		$("msgBar").style.display = "block";
		$("msgBar").style.top = 0;
		setTimeout(function() {
			UploadUI.hideErrorInfo()
		}, HIDE_ERROR_INFO_DELAY_TIME)
	},
	hideErrorInfo : function() {
		var a = $("msgBar").clientHeight + 2;
		$("msgBar").style.top = "-" + a + "px"
	},
	getAccessToken : function(b) {
		var a = UploadUI.getSiteObject(b);
		var c = function(d, e) {
			if (d == "success") {
				UploadUI.getUserInfo(b, e)
			} else {
				var f = null;
				if (e == "access_denied" || e == "invalid_grant") {
					f = e
				}
				if (f) {
					f = chrome.i18n.getMessage(f)
				} else {
					f = e
				}
				UploadUI.showErrorInfo(f);
				UploadUI.showAuth()
			}
		};
		a.getAccessToken(c)
	},
	getUserInfo : function(c, a) {
		var b = UploadUI.getSiteObject(c);
		b.getUserInfo(a, function(d, e, g) {
			if (d == "success") {
				var f = a.id;
				if (!Account.getUser(c, f)) {
					b.currentUserId = f;
					Account.addUser(c, a);
					UploadUI.showUser(c, a);
					UploadUI.fillBoards(g)
				}
			} else {
				UploadUI.showErrorInfo(e)
			}
		})
	},
	getBoards : function(a) {
		Huaban.getBoards(a, function(b, c) {
			if (b == "success") {
				UploadUI.fillBoards(c)
			} else {
				UploadUI.signOut(a)
			}
		})
	},
	fillBoards : function(b) {
		var a = document.querySelector("div.BoardPicker");
		boardPicker = BoardPicker.init(a, b)
	},
	initBoards : function(a) {
		var b = document.querySelector("div.BoardPicker");
		var g = b.querySelector("div.CreateBoard"), j = $("board_name_input"), e = g
				.querySelector("a.btn"), i = g
				.querySelector(".CreateBoardStatus"), c = document
				.querySelector("#bookmarklet .rbtn");
		var h = 8;
		var f = BoardPicker;
		UploadUI.fillBoards(a);
		e.addEventListener("click", function() {
			var k = j.value || "";
			k = k.replace(/^\s+|\s+$/g, "");
			if (k == "") {
				UploadUI.showErrorInfo("请输入名称");
				return false
			}
			e.classList.add("disabled");
			Huaban.createBoard(k, function(l, n) {
				if (l == "success" && n && n.board) {
					f.add(n.board).hide()
				} else {
					if (n.err && n.fieldErrors) {
						for ( var m in n.fieldErrors) {
							UploadUI.showErrorInfo(n.fieldErrors[m][0]);
							break
						}
					} else {
						if (n.err && n.msg) {
							UploadUI.showErrorInfo(n.msg)
						} else {
							UploadUI.showErrorInfo("网络错误，请稍候再试")
						}
					}
				}
				j.value = ""
			})
		});
		c
				.addEventListener(
						"click",
						function() {
							c.classList.add("disabled");
							var l = f.getSelected();
							if (!l) {
								return alert("请选择画板")
							}
							var n = $("description").value;
							var m = document
									.querySelector("input.publish_to_weibo").checked;
							var k = $("url").value;
							var o = UploadUI.getImageData();
							c.querySelector("strong").innerText = "采集中…";
							Huaban
									.upload(
											l,
											n,
											k,
											m,
											o,
											function(p, q, t) {
												if (p == "success") {
													document
															.querySelector("#bookmarklet .pbt").style.display = "none";
													var s = q;
													var r = $("pin-done");
													var v = $("view_pin");
													v.href = "http://" + DOMAIN
															+ "/pins/"
															+ s.pin_id;
													v.onclick = function() {
														chrome.tabs.create({
															url : v.href
														});
														return false
													};
													$("close_window")
															.addEventListener(
																	"click",
																	function() {
																		window
																				.close()
																	});
													r.style.display = "block";
													ShareButton.setPin(s);
													return
												} else {
													if (t && t == 413) {
														UploadUI
																.showErrorInfo(chrome.i18n
																		.getMessage("http_413"))
													} else {
														UploadUI
																.showErrorInfo(q)
													}
												}
												c.classList.remove("disabled");
												c.querySelector("strong").innerText = "采下来"
											})
						});
		var d = function() {
			if (j.value != "") {
				e.classList.remove("disabled")
			} else {
				e.classList.add("disabled")
			}
		};
		j.addEventListener("keydown", d);
		j.addEventListener("focus", d);
		j.addEventListener("blur", d)
	},
	showUser : function(e, c) {
		$("loading").style.display = "none";
		$("authorization").style.display = "none";
		document.querySelector(".pin-form").style.display = "block";
		if (c && c.bindings && c.bindings.weibo) {
			document.querySelector(".pin-form .Buttons label.weibo").style.display = "block"
		} else {
			document.querySelector(".pin-form .Buttons label.weibo").style.display = "none"
		}
		var g = $("userIcon");
		var d = document.createElement("a");
		d.href = Utils.getUserUrl(c);
		d.target = "_blank";
		var b = new Image();
		if (c.avatar) {
			b.src = Utils.getImgUrl(c.avatar, "sq75")
		} else {
			b.src = "/images/default_buddy_icon.jpg"
		}
		var a = document.createElement("span");
		a.title = chrome.i18n.getMessage("remove_auth");
		a.addEventListener("click", function() {
			UploadUI.signOut(c)
		});
		var f = document.createTextNode(c.name);
		d.appendChild(b);
		d.appendChild(f);
		g.appendChild(d);
		g.appendChild(a);
		g.style.display = "block"
	},
	signOut : function(a) {
		var b = $("userIcon");
		b.innerHTML = "";
		b.style.display = "none";
		Account.removeUser(Huaban.siteId, a.id);
		UploadUI.showAuth();
		return false
	},
	showAuth : function() {
		$("loading").style.display = "none";
		$("authorization").style.display = "block";
		document.querySelector(".pin-form").style.display = "none"
	},
	showLoading : function() {
		$("loading").style.display = "block";
		$("authorization").style.display = "none";
		document.querySelector(".pin-form").style.display = "none"
	},
	getImageData : function() {
		photoshop.draw();
		var b = $("canvas").toDataURL("image/png");
		photoshop.finish();
		var a = b.indexOf("data:image/png;base64,");
		if (a != 0) {
			return
		}
		return atob(b.substr(a + 22))
	},
	showDialog : function() {
		var a = $("overlay");
		UI.setStyle(a, {
			width : document.body.scrollWidth + "px",
			height : document.body.scrollHeight + "px"
		});
		UI.show(a);
		UI.show($("pinWrapper"))
	},
	closeDialog : function() {
		var a = $("overlay");
		UI.setStyle(a, {
			width : document.body.scrollWidth + "px",
			height : document.body.scrollHeight + "px"
		});
		UI.hide(a);
		UI.hide($("pinWrapper"))
	}
};
(function() {
	var b;
	chrome.tabs.getSelected(null, function(e) {
		b = e.id
	});
	function c(e) {
		chrome.tabs.update(e, {
			selected : true
		})
	}
	function d(e) {
		chrome.tabs.remove(e)
	}
	function a(i, f, h) {
		var g = UploadUI.sites;
		for ( var j in g) {
			var e = g[j];
			if ((h && j == h) || e.isRedirectUrl(f)) {
				c(b);
				d(i);
				e.parseAccessToken(f);
				return true
			}
		}
		return false
	}
	chrome.extension.onRequest.addListener(function(f, e) {
		switch (f.msg) {
		case "url_for_access_token":
			a(e.tab.id, f.url, f.siteId);
			break
		}
	})
})();